#include <iostream>
#include <cstdio>
#include <thread>
#include <string>
#include <cstdlib>
#include <WinSock2.h> // Windows Sockets API 的头文件，方便使用套接字进行网络通信
#include <Windows.h>


#pragma warning(disable : 4996)
#pragma comment(lib, "ws2_32.lib")

std::string serverip = "1.12.51.69"; // 填写云服务 ip
uint16_t serverport = 8888; // 填写云服务所开放的端口号


int main()
{
	WSADATA wsd; // 保留初始化 返回的信息
	WSAStartup(MAKEWORD(2, 2), &wsd);

	struct sockaddr_in server;
	memset(&server, 0, sizeof(server));

	// 补充信息字段
	server.sin_family = AF_INET;
	server.sin_port = htons(serverport);
	server.sin_addr.s_addr = inet_addr(serverip.c_str());

	SOCKET sockfd = socket(AF_INET, SOCK_DGRAM, 0);

	if (sockfd == SOCKET_ERROR)
	{
		std::cout << "socker error" << std::endl;
		return 1;
	}

	std::string message;
	char buffer[1024];

	while (true)
	{
		std::cout << "Please Enter@ ";
		std::getline(std::cin, message);
		if (message.empty()) continue;
		sendto(sockfd, message.c_str(), (int)message.size(), 0, (struct sockaddr *) &server, sizeof(server));
		
		struct sockaddr_in tmp;
		int len = sizeof(tmp);
		int s = recvfrom(sockfd, buffer, 1023, 0, (struct sockaddr*)&tmp, &len);
		if (s > 0)
		{
			buffer[s] = 0;
			std::cout << buffer << std::endl;
		}
	}

	closesocket(sockfd);
	WSACleanup();
	return 0;
}
